package com.gitee.wsl.ext.array

/**
 * Counts the times given pattern (i.e. an array) can be found in given array
 *
 *
 *
 * **Analysis**
 *
 *  * Time Complexity: `O(n*m)`
 *  * Space Complexity: `O(1)`
 *  * Alters Parameters: `false`
 *
 *
 *
 * @param array   to count in
 * @param pattern to match in array
 * @return number of times pattern is in subject
 */
fun ByteArray.countByteArray(pattern: ByteArray): Int {
    if (pattern.isEmpty() || isEmpty()) {
        return 0
    }
    var count = 0
    outer@ for (i in 0..<size - pattern.size + 1) {
        for (j in pattern.indices) {
            if (this[i + j] != pattern[j]) {
                continue@outer
            }
        }
        count++
    }
    return count
}